﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EntityFramework.Extensions;
using MVCEF.DBContext;
using MVCEF.Models;

namespace MVCEF.Controllers
{
    public class HomeController : Controller
    {
        public static Object thisLock = new Object();//互斥锁

        public ActionResult Index()
        {
            using (var efDBContext = new EfDbContext())
            {
                //var blog = efDBContext.Blog.Where(d=>d.Title != null).ToList();

                SqlParameter[] parameter = { };
                efDBContext.Database.SqlQuery<Blog>("select * from a", parameter).ToList();
            }

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }


        /// <summary>
        /// 记录任务调度日志，以便调试
        /// </summary>
        /// <param name="str"></param> 
        /// <param name="type"></param>
        public  void WriteJobTxt(string str, string type)
        {
            string fileLogPath = AppDomain.CurrentDomain.BaseDirectory + "/Logs/";
            string fileLogName = "";
            fileLogName = type + "_" + DateTime.Now.ToLongDateString() + "_log.txt";
            lock (thisLock)
            {
                FileInfo finfo = new FileInfo(fileLogPath + fileLogName);
                using (FileStream fs = finfo.OpenWrite())
                {
                    //根据上面创建的文件流创建写数据流 
                    StreamWriter strwriter = new StreamWriter(fs);
                    //设置写数据流的起始位置为文件流的末尾 
                    strwriter.BaseStream.Seek(0, SeekOrigin.End);
                    //写入相关记录信息
                    strwriter.WriteLine("发生时间: " + DateTime.Now.ToString() + "=>" + str);
                    //清空缓冲区内容，并把缓冲区内容写入基础流 
                    strwriter.Flush();
                    strwriter.Close();
                    fs.Close();
                }
            }
        }

        /// <summary>
        /// 将异常打印到LOG文件
        /// </summary>
        /// <param name="ex">异常</param>
        /// <param name="LogAddress">日志文件地址</param>
        public  void WriteBugLog(Exception ex)
        {

            string fileLogPath = AppDomain.CurrentDomain.BaseDirectory + "/Logs/";
            string fileLogName = "";
            fileLogName = "Bug_" + DateTime.Now.ToLongDateString() + "_log.txt";

            lock (thisLock)
            {
                FileInfo finfo = new FileInfo(fileLogPath + fileLogName);
                using (FileStream fs = finfo.OpenWrite())
                {
                    //把异常信息输出到文件
                    StreamWriter strwriter = new StreamWriter(fs);
                    strwriter.WriteLine("当前时间：" + DateTime.Now.ToString());
                    strwriter.WriteLine("异常信息：" + ex.Message);
                    strwriter.WriteLine("异常对象：" + ex.Source);
                    strwriter.WriteLine("调用堆栈：\n" + ex.StackTrace.Trim());
                    strwriter.WriteLine("触发方法：" + ex.TargetSite);
                    strwriter.WriteLine();
                    strwriter.Close();
                }
            }
        }
    }
}